import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/routes/route.dart';

class AsrAppBar extends AppBar {
  AsrAppBar({
    Key? key,
    Widget? leading,
    bool automaticallyImplyLeading = true,
    Widget? title,
    List<Widget>? actions,
    Widget? flexibleSpace,
    PreferredSize? bottom,
    double elevation = 0,
    Color? shadowColor,
    Color barColor = Colors.black,
    ShapeBorder? shape,
    Color? backgroundColor,
    Brightness? brightness,
    IconThemeData? iconTheme,
    IconThemeData? actionsIconTheme,
    TextTheme? textTheme,
    bool primary = true,
    bool centerTitle = true,
    bool excludeHeaderSemantics = false,
    double titleSpacing = NavigationToolbar.kMiddleSpacing,
    double toolbarOpacity = 1.0,
    double bottomOpacity = 1.0,
    double? toolbarHeight,
    double? leadingWidth,
    String? titleText,
    Function? leadFun,
    bool isLead = true,
    SystemUiOverlayStyle? systemOverlayStyle,
  }) : super(
          key: key,
          leading: isLead == true
              ? leading == null
                  ? _defaultLead(leadFun!,barColor: barColor)
                  : leading
              : null,
          automaticallyImplyLeading: automaticallyImplyLeading,
          title: titleText != null ? _defaultTitle(titleText,barColor: barColor) : title,
          actions: actions,
          flexibleSpace: flexibleSpace,
          bottom: bottom,
          elevation: elevation,
          shadowColor: shadowColor,
          shape: shape,
          backgroundColor: backgroundColor,
          iconTheme: iconTheme,
          actionsIconTheme: actionsIconTheme,
          primary: primary,
          centerTitle: centerTitle,
          excludeHeaderSemantics: excludeHeaderSemantics,
          titleSpacing: titleSpacing,
          toolbarOpacity: toolbarOpacity,
          bottomOpacity: bottomOpacity,
          toolbarHeight: toolbarHeight,
          leadingWidth: leadingWidth,
          systemOverlayStyle: systemOverlayStyle,
        );
}

Widget _defaultTitle(String title, {Color barColor = Colors.black}) {
  return Container(
    child: Text(
      title,
      style: TextStyle(
          color: barColor, fontSize: 36.sp, fontWeight: FontWeight.w500),
    ),
  );
}

Widget _defaultLead(Function leadFun,{Color barColor = Colors.black}) {
  return GestureDetector(
    onTap: () {
      if (leadFun != null) {
        leadFun();
      } else {
        Navigator.of(ASRRoute.navigatorKey.currentContext!).pop(false);
      }
    },
    child: Container(
      child: Icon(
        Icons.arrow_back_ios,
        size: 44.w,
        color: barColor,
      ),
    ),
  );
}
